<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class Room_section_subject Extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('M_sections','M_levels'));
		$this->load->helper('misc');
		$this->_init_pagnate_config();
		$this->view_data['system_message'] = $this->_msg();
	}
	
	/*
	+======================================================================
	+
	+	SCHEDULE SCHEDULE SCHEDULE SCHEDULE SCHEDULE SCHEDULE
	+======================================================================
	*/
	
	public function schedule_by_section($section_id = false)
	{
		if($section_id !== false AND is_numeric($section_id))
		{
			$this->load->model(array('M_schedule','M_subjects','M_sections','M_employees'));
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			$this->view_data['schedules'] = $this->M_schedule->get_all_schedules_from_section_id($section_id);
			$this->view_data['section'] = $this->M_sections->get_section($section_id);
			$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
			$this->view_data['section_id']	= $section_id;
			$this->view_data['system_message'] = $this->_msg();;

			if($this->input->post('update_schedule')){
				$sched = $this->input->post('sched');
				if($sched){

					$ctr = 0;

					foreach ($sched as $s_id => $s) {
						$xrs_upd = $this->M_schedule->update($s_id, $s);	
						// vd($s);
						if($xrs_upd){
							$ctr++;
						}
					}

					if($ctr > 0){
						$this->_msg('s','All Schedules were successfully updated', current_url());
					}
				}

				$this->_msg('e','No schedule to be updated', current_url());
			}

		}else{
			show_404();
		}
	}
	
	public function add_schedule($section_id = FALSE)
	{
		if($section_id !== FALSE AND is_numeric($section_id))
		{
			$section_id = intval($section_id);
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			$this->load->model(array('M_schedule','M_sections','M_employees'));
			$this->view_data['subjects'] = $this->M_schedule->get_subjects_for_section_and_grade_level($section_id);
			$this->view_data['section_profile'] = $this->M_sections->get_section($section_id);
			$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
			$this->view_data['section_id'] = $section_id;
			$this->view_data['schedules'] = $this->M_schedule->get_all_schedule_by_section($section_id);
			
			
			if($this->input->post('add_schedule'))
			{
				$this->form_validation->set_rules('subject_id','Subject','required');
				$this->form_validation->set_rules('t_s','Time Start','required');
				$this->form_validation->set_rules('t_e','Time End','required');
				$this->form_validation->set_rules('days','Days','required');
				$this->form_validation->set_rules('teacher','Teacher In-Charge','required');
				
				if($this->form_validation->run() !== FALSE)
				{
					$input['subject_id'] = $this->input->post('subject_id',TRUE);
					$input['time_from'] = $this->input->post('t_s',TRUE);
					$input['time_to'] = $this->input->post('t_e',TRUE);
					$input['day'] = $this->input->post('days',TRUE);
					$input['teacher_id'] = $this->input->post('teacher',TRUE);
					$input['section_id'] = $this->input->post('section_id');
					
					$this->load->model('M_schedule');
					$result = (object)$this->M_schedule->add_schedule($input);
					if($result->status == 'true')
					{
						$this->session->set_flashdata('system_message','<div class="success">'.$result->msg.'</div>');
						redirect('rss-add-schedule/'.$input['section_id']);
					}else{
						$this->view_data['system_message'] = '<div class="error">'.$result->msg.'</div>';
					}
				}
			}
		}else{
			show_404();
		}
	}
	
	public function update_schedule($schedule_id = false,$s_id = FALSE)
	{
		if($schedule_id !== false AND is_numeric($schedule_id) AND $s_id)
		{
			$section_id = intval($schedule_id);
			$this->load->model(array('M_schedule','M_sections','M_employees'));
			$this->view_data['section_profile'] = $this->M_sections->get_section($section_id);
			$this->view_data['schedules'] = $p = $this->M_schedule->get_all_schedule($section_id);
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			$this->view_data['subjects'] = $this->M_schedule->get_subjects_for_section_and_grade_level($p->section_id);
			$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
			$this->view_data['schedule_id'] = $section_id;
			$this->view_data['section_id'] = $s_id;

			if($this->input->post('update_schedule'))
			{
				$this->form_validation->set_rules('t_s','Time Start','required');
				$this->form_validation->set_rules('t_e','Time End','required');
				$this->form_validation->set_rules('days','Days','required');
				$this->form_validation->set_rules('teacher','Teacher In-Charge','required');
				
				if($this->form_validation->run() !== FALSE)
				{
					$input['subject_id'] = $this->input->post('subject_id',TRUE);
					$input['time_from'] = $this->input->post('t_s',TRUE);
					$input['time_to'] = $this->input->post('t_e',TRUE);
					$input['day'] = $this->input->post('days',TRUE);
					$input['teacher_id'] = $this->input->post('teacher',TRUE);
					$schedule_id = $this->input->post('schedule_id');

					
					$this->load->model('M_schedule');
					$result = (object)$this->M_schedule->update_schedule($input,$schedule_id);
					if($result->status == 'true')
					{
						$this->_msg('s',$result->msg,'rss-update-schedule/'.$schedule_id.'/'.$s_id);
					}else{
						$this->view_data['system_message'] = $this->_msg('e',$result->msg);
					}
				}
			}		
		}else{
			show_404();
		}
	}

	public function delete_schedule($schedule_id = false,$s_id = FALSE)
	{
		if($schedule_id !== false AND is_numeric($schedule_id) AND $s_id)
		{
			$section_id = intval($schedule_id);
			$this->load->model(array('M_schedule','M_sections','M_employees'));
			$this->view_data['section_profile'] = $this->M_sections->get_section($section_id);
			$this->view_data['schedules'] = $p = $this->M_schedule->get_all_schedule($section_id);
			$this->view_data['system_message'] = $this->session->flashdata('system_message');
			$this->view_data['subjects'] = $this->M_schedule->get_subjects_for_section_and_grade_level($p->section_id);
			$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
			$this->view_data['schedule_id'] = $section_id;
			$this->view_data['section_id'] = $s_id;
			if(!$p){ show_404(); }
			$result = (object)$this->M_schedule->del_sched($schedule_id);
			if($result)
			{
				$this->_msg('s','Delete Successfull','rss-section-schedule/'.$s_id);
			}else{
				$this->_msg('s','Delete unsuccessfull','rss-section-schedule/'.$s_id);
			}

		}else{
			show_404();
		}
	}

	/**
	 * Generate All Section Schedules
	 * Generate All Schedule of sections without the schedule based form the subjects in the curricullum
	 */
	public function generate_all_schedules()
	{
		$this->load->model('M_schedule');
		$this->load->model('M_curriculum');


		$sections = $this->M_sections->get_all_sections();

		if($sections){
			$ctr = 0;
			foreach ($sections as $key => $sec) {
				// vd($sec);

				$level_id = $sec->grade_level_id;

				/* Get Schedule if available */
				$schedules = $this->M_schedule->get_all_schedule_by_section($sec->section_id);
				if(!$schedules){ #if false then create schedule
					
					/* Get Subjects curriculum by level */					
					$curriculum_subjects = $this->M_curriculum->get_curriculum_by_level($level_id);
					if($curriculum_subjects){

						foreach ($curriculum_subjects as $ck => $subj) {
							unset($data);
							$data['subject_id'] = $subj->subject_id;
							$data['section_id'] = $sec->section_id;
							$data['is_deleted'] = 0;
							$xrs_add = $this->M_schedule->insert($data);
							if($xrs_add){
								$ctr++;
							}
						}
					}
				}
			}
			
			if($ctr > 0){

				$this->_msg('s',"There are $ctr schedule(s) were successfully created", 'rss-section');
			}
		}

		$msg = "
			<p>No schedule was created because of the ff:</p>
			<p>All sections already have schedules.</p>
			<p>No section available.</p>
			<p>No curriculum or no subjects in the curriculum created.</p>
		";

		$this->_msg('e',$msg, 'rss-section');
	}
	
	function restore_schedule($id)
	{
			if($id == '' OR $id == FALSE OR !(int)$id )
			{
					redirect('admin');
			}else
			{
					if($this->M_schedule->undestroy($id) == TRUE)
					{
							$this->session->set_flashdata('system_message','<div class="success">Data was Activated</div>');
							redirect('rss-schedule');  
					}else
					{
							$this->session->set_flashdata('system_message','<div class="error">Unable to destroy Data</div>');
							redirect('rss-schedule');
					}
			}
	}
	
	/*
	+======================================================================
	+
	+	SECTIONS  SECTIONS SECTIONS SECTIONS SECTIONS SECTIONS
	+======================================================================
	*/
	
	public function add_section()
	{
		$this->load->model('M_employees');
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
		
		if($this->form_validation->run('grade_level_section') == FALSE)
		{
		
		}
		else
		{	
			$input['section_name'] 	 = $this->input->post('section_name',TRUE);
			$input['grade_level_id'] = 	$this->input->post('section_grade_level',TRUE);
			$input['adviser_id'] 	 = $this->input->post('section_employee_name',TRUE);
			$input['max_load'] 	 = $this->input->post('max_students',TRUE);
			$result = $this->M_sections->add_grade_level_section($input);
			if($result['status'] == 'true')
			{
				if($this->M_employees->update_adviser_status($input['adviser_id']) == TRUE)
				{
					$this->_msg('s',$result['msg'],'rss-add-section');
				}else{
					$this->_msg('n',$result['msg'],'rss-add-section');
				}
			}else
			{
				$this->_msg('e',$result['msg'],'rss-add-section');		
			}
		}
	}
	
	public function sections()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		
		if($this->input->post('filter_sections'))
		{
			/* If post filter*/
			$id = $this->input->post('i');
			if(!$id == '' AND is_numeric($id))
			{
				$this->view_data['grade_level_section_data'] = $this->M_sections->fetch_grade_level_section(FALSE,FALSE,TRUE,$id);
			}else
			{
				redirect('rss-section');
			}
			$this->view_data["links"] = NULL;
		}else{
			/* pagenation */
			$this->load->library('pagination');
			$this->pagnate_config['base_url'] = base_url().'rss-section';
			$this->pagnate_config['total_rows'] = $this->M_sections->count_grade_level_section();
			$this->pagnate_config['per_page'] = 15;
			$this->pagnate_config["uri_segment"] = 2;
			$this->pagination->initialize($this->pagnate_config);

			$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
			$this->view_data['grade_level_section_data'] = $this->M_sections->fetch_grade_level_section($this->pagnate_config["per_page"], $page);
			$this->view_data["links"] = $this->pagination->create_links();
		}
	}
	
	function update_section($id='')
	{
		$this->load->model('M_employees');
		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		$this->view_data['teachers'] = $this->M_employees->get_all_employees_array('teacher');
		$this->view_data['system_message'] = $this->_msg();
	
		if($id != FALSE OR $id != '' OR (int)$id)
		{
			$this->view_data['grade_level_section_data'] = $this->M_sections->get_all_sections($id);
			
			if($_POST)
			{
				$input['section_name'] 		= $this->input->post('section_name',TRUE);
				$input['grade_level_id'] 	= $this->input->post('section_grade_level',TRUE);
				$input['adviser_id'] 		= $this->input->post('section_employee_name',TRUE);
				$input['max_load'] 			= $this->input->post('max_student',TRUE);
				$input['updated_at'] 		= NOW;
				if($this->form_validation->run('grade_level_section') == FALSE)
				{
					$this->view_data['grade_level_section_data'] = $this->M_sections->get_all_sections($id);
				}
				else
				{
					if($this->M_sections->update_grade_level_section($input,$id)['status'] == 'true')
					{
						$this->_msg('s','Update of section was successful','rss-update-section/'.$id);
					}
					elseif($this->M_sections->update_grade_level_section($input,$id)['status'] == 'exist')
					{
						$this->_msg('e','Data already exists','rss-update-section/'.$id);
					}
					elseif($this->M_sections->update_grade_level_section($input,$id)['status'] == 'false')
					{
						$this->_msg('s','An error occured while Updating, Please Try again!','rss-update-section/'.$id);	
					}
					
					$this->view_data['grade_level_section_data'] = $this->M_sections->get_all_sections($id);
				}
			}
		}
	}
	
	/*
		Destroys Section Data
		12/12/2012
	*/
	function destroy_section($id)
	{
		if($id == '' OR $id == FALSE OR !(int)$id )
		{
			redirect('');
		}else
		{
			if($this->M_sections->destroy($id) == TRUE)
			{
				$this->_msg('s','Section Successfully Deleted','rss-section');
			}
			else
			{
				$this->_msg('e','Unable to Delete section and Data','rss-section');
			}
		}
	}
	
	/*
		Undestroys Section
		12/12/2012
	*/
	function undestroy_section($id)
	{
		if($id == '' OR $id == FALSE OR !(int)$id )
		{
			redirect('');
		}else
		{
			if($this->M_sections->undestroy($id) == TRUE)
			{
				$this->_msg('s','rss-section','Data was Successfully restored.');
			}
			else
			{
				$this->_msg('e','rss-section','Unable to restore selected Data.');
			}
		}		
	}
	
	/*
	+======================================================================
	+
	+	ROOMS ROOMS ROOMS ROOMS ROOMS ROOMS
	+======================================================================
	*/
	
	public function rooms()
	{
		$this->load->model(array('M_rooms','M_levels'));

		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		$id = intval($this->input->post('i'));
		if($this->input->post())
		{
			if(empty($id))
			{
				$this->view_data['rooms'] = $this->M_rooms->get_room_section_level();
			}else{
				$this->view_data['rooms'] = $this->M_rooms->get_room_section_level($id);
			}
			
		}else{
			$this->view_data['rooms'] = $this->M_rooms->get_room_section_level();
		}
	}
	
	
	public function view_room_data($room_id = FALSE)
	{
		if($room_id !== FALSE AND is_numeric($room_id))
		{
			$this->load->model('M_rooms','r');
			$this->load->model('M_schedule','s');
			$this->view_data['room_sect_profile']= $p = $this->r->get_room_section($room_id);
			$this->view_data['students_enrolled'] = $this->r->get_room_section_students($room_id);
			// $this->view_data['subject_schedule']  = $this->s->get_all_schedule_by_section($p->section_id);
		}else{
			show_404();
		}
	}
	
 
	function edit_room($id = '')
	{
		$this->view_data['system_message'] = $this->_msg();
		$id = is_numeric($id) ? intval($id) : redirect('admin/rooms');
		$this->load->model('M_rooms');
		$this->view_data['levels'] = $this->M_levels->get_all_levels();
		$this->view_data['sections'] = $this->M_sections->get_all_sections();
		$this->view_data['room'] = $this->M_rooms->get_room_section($id);
		$this->view_data['room_id'] = $id;
		
		if($this->input->post('save'))
		{
			$this->form_validation->set_rules('room_name', 'Room Name', 'required');
			$this->form_validation->set_rules('room_desc', 'Room Description', 'required');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->view_data['system_message'] = '<div class="alert-box alert">'.validation_errors().'</div>';
			}
			else {
				$this->load->model('M_rooms');
				$v = (object) $this->input->post();
				$room_data['name'] = $v->room_name;
				$room_data['description'] = $v->room_desc;
				$room_data['updated'] = NOW;
				$room_data['updated_by'] = $this->session_user;
				
				if(isset($v->section))
				{
					$room_data['section_id'] = $v->section;
				}
				
				if($this->M_rooms->edit_room($room_data, $v->room_id))
				{
					$this->_msg('s','Room was successfully Updated','rss-edit-room/'.$id);
				}
				else{
					$this->view_data['system_message'] = '<div class="error">Update Failed</div>';
				}
			}
		}
	}
	
	
	function add_room()
	{
		$this->view_data['auth'] = $this->authlink-> generate_authlink();
		$this->view_data['sections'] = $this->M_sections->get_all_sections();
		$this->view_data['system_message'] = $this->_msg();
		if($this->input->post('add'))
		{
			#$this->form_validation->set_rules('level_id', 'Room Assignment', 'required');
			$this->form_validation->set_rules('section_id', 'Section Assignment', 'required');
			$this->form_validation->set_rules('name', 'Room Name', 'required');
			$this->form_validation->set_rules('description', 'Room Description', 'required');
			
			
			if($this->form_validation->run() == FALSE){
				$this->view_data['system_message'] = validation_errors();
			}else
			{
				$this->load->model('M_rooms');
				$v = (object) $this->input->post();
				$room_data['name'] = $v->name;
				$room_data['description'] = $v->description;
				$room_data['section_id'] = $v->section_id;
				$room_data['created'] = NOW;
				$room_data['created_by'] = $this->session_user;
				if($this->M_rooms->add_room($room_data) == TRUE)
				{
					if($this->M_sections->set_sections_room_assigned($v->section_id))
					{
						if($this->M_sections->update_section_room_assigned_status($v->section_id))
						{
							$this->_msg('s','Successfully added new room and assigned section.','rss-add-room');
						}else{
							$this->_msg('s','Successfully Added new room and assigned section but failed to update room status.','rss-add-room');
						}
					}else
					{
						$this->_msg('e','Sorry unable to add new room.','rss-add-room');
					}
				}else
				{
					$this->_msg('e','Sorry unable to add new room.','rss-add-room');
				}
			}
		}
	}
	
	
	public function view_section_students($x = FALSE)
	{
		$this->view_data['system_message'] = $this->_msg();
		if($x == FALSE AND !is_numeric($x))
		{
			redirect();
		}else{
			$this->load->model('M_sections','msec');
			$this->load->model('M_schedule','s');
			$this->load->model('M_employees','memp');
			$this->view_data['section_profile'] = $this->M_sections->get_section($x);
			$this->view_data['students'] = $this->msec->get_all_students_in_section($x);
			$this->view_data['schedules']  = $this->s->get_all_schedule_by_section($x);
			$this->view_data['section_id']  = $x;
			$this->view_data['teachers'] = $this->memp->get_all_employees_array('teacher');
		}
	}
	
	/*
	+======================================================================
	+
	+	curriculum curriculum curriculum curriculum curriculum curriculum
	+======================================================================
	*/
	
	public function set_curriculum($id = FALSE)
	{
		if($id == FALSE AND !is_numeric($id))
		{
			show_error('Sorry, we are unable to process the page you desired.',404);
		}else{
			$this->view_data['system_message'] = $this->_msg();
			$this->load->model('M_curriculum','mcu');
			$this->view_data['subjects'] = $this->mcu->get_subjects_available_for_curriculum($id);
			$this->view_data['details'] = $this->M_levels->get_grade_level($id);
			$this->view_data['selected'] = $this->mcu->get_selected_subjects_for_curriculum($id);
			$this->view_data['level_id'] = $id;
			
			
			if($this->input->post('set_curriculum'))
			{
				$this->form_validation->set_rules('subjects[]','Subjects','required');
				$this->form_validation->set_message('required', 'Please choose a subject from the list below by clicking on the box.');
				if($this->form_validation->run() !== FALSE)
				{
					$post_data = $this->input->post('subjects');
					$result = $this->mcu->set_curriculum_for_level($id,$post_data);
					if($result == TRUE)
					{
						$this->_msg('s','Successfully added new subjects to grade level.','set-curriculum/'.$id);
					}else{
						$this->_msg('e','An error was encountered while setting subjects for grade level.','set-curriculum/'.$id);
					}
					
				}else
				{
					if(validation_errors() !== '')
					{
						$this->view_data['system_message'] = $this->_msg('e',validation_errors());
					}
				
				}
			}
		}
	}
	
	public function curriculum()
	{
		$this->load->model('M_curriculum','mcu');
		$curriculums = $this->M_levels->get_specific_coloumn('level_id,level_desc,level_code,level_category');
		$this->view_data['levels'] = arrayToObject($curriculums);
		$this->view_data['curriculum'] = function($x){return $this->mcu->get_level_curriculum($x);};
	}
	
	public function delete_subject_from_curriculum($hash)
	{
		$this->load->model('M_curriculum','mcu');
		
		$x = $this->hs->decrypt($hash);

		if($x)
		{
			$id = $x[0];
			$level_id = $x[1];
			
			$result = $this->mcu->remove_from_curriculum($id);
			if($result)
			{
				$this->_msg('s','Subject removed from curricullum.','set-curriculum/'.$level_id);
			}else{
				$this->_msg('e','Unable to removed subject from curricullum. Try again.','set-curriculum/'.$level_id);
			}
		}else{
			show_error('Invalid URL Parameters',404);
		}
	}
	
	public function get_subject_curriculum($section_id)
	{
		$result = $this->M_sections->check_section_and_get_curriculum($section_id);
	
		if($result->status == 'true')
		{
			$this->_msg('s',$result->msg,'rss-section-detail/'.$section_id);
		}else{
			$this->_msg('e',$result->msg,'rss-section-detail/'.$section_id);
		}
	}
	
	/*
	+======================================================================
	+
	+	SUBJECT SUBJECT SUBJECT SUBJECT SUBJECT SUBJECT SUBJECT
	+======================================================================
	*/
	
	/*=========================================== SUBJECT =================================================*/
	function subjects()
	{
		$this->load->model(array('M_subjects','M_levels'));
		
		if($this->uri->segment(3) == 'activate' && $this->uri->segment(4) != '')
			$this->M_subjects->activate($this->uri->segment(4), NOW);
		else if($this->uri->segment(3) == 'deactivate' && $this->uri->segment(4) != '')
			$this->M_subjects->deactivate($this->uri->segment(4), NOW);
			
		$this->view_data['levels'] = $this->M_levels->get_all_levels();
		
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'room_section_subject/subjects';
		$this->pagnate_config['per_page'] = 15;
		
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		if($this->input->post('search'))
		{
			$this->session->set_userdata('search_strings','');
			foreach($this->input->post('search_strings') as $k => $v)
			{
				if(!empty($v))
				{
					$post_result[$k] = $v;
				}
			}
			$search_strings = isset($post_result) ? $post_result : array('subj_desc'=>'');
			$this->session->set_userdata('search_strings',$search_strings);
			$search_string = $this->session->userdata('search_strings');
			$this->pagnate_config['total_rows'] = $this->M_subjects->count_fetch_subjects($search_string);
			$this->view_data['subjects'] = $this->M_subjects->fetch_subjects($search_string,$this->pagnate_config["per_page"], $page);
		}else{
			if($this->session->userdata('search_strings') !== FALSE)
			{
				$search_string = $this->session->userdata('search_strings');
				$this->pagnate_config['total_rows'] = $this->M_subjects->count_fetch_subjects($search_string);
				$this->view_data['subjects'] = $this->M_subjects->fetch_subjects($search_string,$this->pagnate_config["per_page"], $page);
			}else
			{
				$this->pagnate_config['total_rows'] = count($this->M_subjects->get_all_subjects());
				$this->view_data['subjects'] = $this->M_subjects->fetch_subjects(false,$this->pagnate_config["per_page"], $page);
			}
		}
		$this->pagination->initialize($this->pagnate_config);
		$this->view_data["links"] = $this->pagination->create_links();
	}
	
	function add_subject($level_id = FALSE)
	{
		$this->view_data['system_message'] = $this->_msg();	
		$this->view_data['selected_grade_level_id'] = $level_id;
		$this->load->Model(array('M_school_years','M_gperiods'));
		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		$this->view_data['academic_year'] = $this->M_school_years->get_all_school_year_array();
		$this->view_data['level_id'] = $level_id;
		
		if($this->input->post('add'))
		{
			$this->form_validation->set_rules('grade_level_id', 'Grade Level', 'required');
			$this->form_validation->set_rules('code', 'Subject Code', 'required');
			$this->form_validation->set_rules('description', 'Subject Description', 'required');
			// $this->form_validation->set_rules('academic_year', 'Academic Year', 'required');
			// $this->form_validation->set_rules('subj_uniq_id', 'Unique ID', 'required|is_unique[subjects.subj_uniq_id]');
			$this->form_validation->set_rules('subject_unit', 'Subject Unit', 'required|numeric');
			$this->form_validation->set_message('is_unique', 'Unique Alphanumeric ID Already Taken');

			if($this->form_validation->run() == FALSE)
			{
				$this->view_data['system_message'] = '<span style="color:#f00;">'.validation_errors().'</span>';

				// echo validation_errors();
			}else{
				$this->load->model('M_subjects');
				$subject_data['subj_code'] = $this->input->post('code');
				$subject_data['subj_desc'] = $this->input->post('description');
				$subject_data['subj_stat'] = $this->input->post('subj_stat');
				$subject_data['subject_name'] = $this->input->post('subject_parent');
				$subject_data['grade_level_id'] = $this->input->post('grade_level_id');
				$subject_data['subj_uniq_id'] = $this->input->post('subj_uniq_id');
				$subject_data['subj_units'] = $this->input->post('subject_unit');
				$subject_data['subj_created'] = NOW;
				$subject_data['children'] = $this->input->post('subject_children');
				
				if($this->M_subjects->add_subject($subject_data))
				{
					$this->_msg('s','Successfully added subject with subject code ('.$this->input->post('code').') to database.','room_section_subject/add_subject');
				}else{
					$this->_msg('e','Unable to add subject with subject code ('.$this->input->post('code').') to database.','room_section_subject/add_subject');
				}
			}
		}else{

			
		}
	}
	
	function edit_subject($id = '')
	{
			$id = is_numeric($id) ? intval($id) : redirect('');

			$this->load->Model(array('M_subjects','M_school_years','M_gperiods'));
			
			$this->view_data['subject'] = $this->M_subjects->get_subject($id);
			$this->view_data['academic_year'] = $this->M_school_years->get_all_school_year_array();
			$this->view_data['grading_period'] = $this->M_gperiods->get_all_grading_period_array();
			$this->view_data['levels'] = $this->M_levels->get_all_levels();
	
			$this->form_validation->set_rules('grade_level_id', 'Grade Level', 'required');
			$this->form_validation->set_rules('code', 'Subject Code', 'required');
			$this->form_validation->set_rules('description', 'Subject Description', 'required');
			// $this->form_validation->set_rules('academic_year', 'Academic Year', 'required');
			// $this->form_validation->set_rules('subj_uniq_id', 'Subject Unque ID', 'required|alpha_dash');
			$this->form_validation->set_rules('subject_unit', 'Subject Unit', 'required|numeric');
			$this->form_validation->set_message('is_unique', 'Unique Alphanumeric ID Already Taken');
			
		 
		if($this->input->post('save'))
		{
			$this->form_validation->set_rules('code', 'Subject Code', 'required');
			$this->form_validation->set_rules('description', 'Subject Description', 'required');
			
			if($this->form_validation->run() == FALSE)
			{
				$this->view_data['system_message'] = '<div class="error">'.validation_errors().'</div>';
			}
			else{
				$this->load->model('M_subjects');
				$subject_data['subj_code'] = $this->input->post('code');
				$subject_data['subj_desc'] = $this->input->post('description');
				$subject_data['subj_stat'] = $this->input->post('subj_stat');
				$subject_data['grade_level_id'] = $this->input->post('grade_level_id');
				$subject_data['subj_units'] = $this->input->post('subject_unit');
				$subject_data['subj_uniq_id'] = $this->input->post('subj_uniq_id');
				$subject_data['subject_name'] = $this->input->post('subject_parent');
				$subject_data['subj_updated'] = NOW;
				$subject_data['children'] = $this->input->post('subject_children');

				if($this->M_subjects->edit_subject($subject_data, $this->input->post('subj_id')))
				{
					$this->view_data['system_message'] = '<div class="success">Subject edited successfully</div>';
					$this->_msg('s','Success. Subject record updated.','room_section_subject/edit_subject/'.$id);
				}
				else{
					$this->_msg('e','Error occured. Unable to edit subject.','room_section_subject/edit_subject/'.$id);
				}
			}
		}
	}
	
	/*
	+======================================================================
	+
	+	GRADELEVEL GRADELEVEL GRADELEVEL GRADELEVEL GRADELEVEL GRADELEVEL
	+======================================================================
	*/
	
	function grade_level()
	{
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		/* pagenation */
		$this->load->library('pagination');
		$this->pagnate_config['base_url'] = base_url().'room_section_subject/grade_level';
		$this->pagnate_config['total_rows'] = $this->M_levels->count_grade_level();
		$this->pagnate_config['per_page'] = 20;
		$this->pagination->initialize($this->pagnate_config);

		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->view_data['grade_level_data'] = $this->M_levels->fetch_grade_level($this->pagnate_config["per_page"], $page);
        $this->view_data["links"] = $this->pagination->create_links();

	}
	
	function update_grade_level()
	{
		$this->view_data['system_message'] = $this->session->flashdata('system_message');
		$this->view_data['grade_level_data'] = $this->M_levels->get_all_levels();
		$this->view_data['status_opt'] = array('active'=>'Active','inactive'=>'Inactive');
		$this->view_data['category_opt'] = array( ''=>'Select Category',
						'k12 Category' => array(
							'preschool'=>'Pre School',
							'lowschool'=>'Lower School',
							'uppschool'=>'Upper School',
							'gradeschool'=>'Grade School',
							'highschool'=>'High School'
						));
		$this->view_data['level_heiarchy_opt'] = $this->M_levels->create_heiarchy_options();
		if($this->input->post('update_grade_levels'))
		{
			if($this->M_levels->update_grade_level($this->input->post('grade_levels')))
			{
				$this->_msg('s','Update Of grade levels was successful.','room_section_subject/update_grade_level');
			}else{
				$this->view_data['system_message'] = '<div class="error">An Error occured while updating data.</div>';
			}
		}
	}
	
	function add_grade_level()
	{
		$this->view_data['system_message'] = $this->session->flashdata('system_message');

		if($this->form_validation->run('grade_level') == FALSE)
		{

		}else
		{	
			$input['level_desc'] = $this->input->post('level_desc',TRUE);
			$input['level_code'] = $this->input->post('level_code',TRUE);
			$input['level_stat'] = $this->input->post('level_stat',TRUE);
			$input['level_code_for_id'] = $this->input->post('level_id_code',TRUE);
			$input['level_category'] = $this->input->post('level_cat',TRUE);
			
			
			$result = $this->M_levels->add_grade_level($input);
			
			if($result->status == 'true')
			{
				$this->_msg('s',$result->message,'room_section_subject/grade_level');
			}elseif($result->status == 'exists')
			{
				$this->view_data['system_message'] = $this->_msg('e',$result->message);					
			}elseif($result->status == 'false')
			{
				$this->view_data['system_message'] = $this->_msg('e',$result->message);					
			}
		}
		
	}
	
	function destroy_gl($id)
	{
		if($id == '' OR $id == FALSE OR !(int)$id )
		{
			redirect('admin');
		}else
		{
			$result =  $this->M_levels->destroy($id);
			if($result->status == TRUE)
			{
				$this->_msg('s',$result->log,'room_section_subject/grade_level');
			}else
			{
				$this->_msg('e',$result->log,'room_section_subject/grade_level');
			}
		}
	}
	
	function delete_grade_level($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x)
		{
			$id = $x[0];
			
			if($this->M_levels->delete_grade_level($id))
			{
				$this->_msg('s','Level Successfully Removed','room_section_subject/grade_level');
			}else{
				$this->_msg('e','Unable to remove Level.','room_section_subject/grade_level');
			}

		}else{
			show_404();
		}
	}
	
	function undestroy_gl($id)
	{
		if($id == '' OR $id == FALSE OR !(int)$id )
		{
			redirect('admin');
		}else
		{
			if($this->M_levels->undestroy($id) == TRUE)
			{
				$this->_msg('e','Data was Activated','room_section_subject/grade_level');
				
			}else
			{
				$this->_msg('e','Unable to Activate Data','room_section_subject/grade_level');
			}
		}		
	}

	/*
	+======================================================================
	+
	+	BLOCKSECTION BLOCKSECTION BLOCKSECTION BLOCKSECTION BLOCKSECTION
	+======================================================================
	*/
	
	public function block_section_settings()
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('m_sections','msec');
		$this->view_data['block'] = $this->msec->show_all_block();
		$this->view_data['levels'] = $this->M_levels->get_all_levels_array();
		$this->view_data['sections'] = $this->msec->show_section_with_block();

		
		if($this->input->post('create_block_section'))
		{
			$this->form_validation->set_rules('level','Grade Level','required|trim|htmlspecialchars');
			$this->form_validation->set_rules('block_name','Block Name','required|trim|htmlspecialchars|strip_tags');
			$this->form_validation->set_rules('block_desc','Block Desc','trim|htmlspecialchars|strip_tags');
			
			if($this->form_validation->run() !== FALSE)
			{
				$this->load->model('M_sections','msec');
				$input['block_level'] = $this->input->post('level');
				$input['block_name'] = $this->input->post('block_name');
				$input['block_desc'] = $this->input->post('block_desc');
				
				$result = $this->msec->insert_block_section($input);
				
				if($result->status == 'true')
				{
					$this->_msg('s',$result->log,'room_section_subject/block_section_settings#create-block');
				}elseif($result->status == 'false')
				{
					$this->_msg('e',$result->log,'room_section_subject/block_section_settings#create-block');
				}
			}else{
				$this->view_data['system_message'] = validation_errors();
			}
		}
		
	}
	
	public function add_section_to_block($raw)
	{
		$this->view_data['system_message'] = $this->_msg();
		$this->load->model('m_sections','msec');
		$x = $this->hs->decrypt($raw);
		if($x)
		{
			$lid = $x[0];
			$bid = $x[1];
			$this->view_data['sections'] = $this->msec->get_section_for_block($lid);
			$this->view_data['block_desc'] = $this->msec->show_section_desc($bid);
			$this->view_data['block_id'] = $bid;
			$this->view_data['level_id'] = $lid;
			
		}else{
		
			if($this->input->post('add_section_to_block'))
			{
				$this->form_validation->set_rules('section[]','','required|trim|htmlspecialchars|numeric');
			
				if($this->form_validation->run() !== FALSE)
				{
					$id_post = $this->input->post('block_id');
					$lv_post = $this->input->post('level_id');
					$raw_id = $this->hs->encrypt(intval($lv_post),intval($id_post));
					$this->view_data['sections'] = $this->msec->get_section_for_block($id_post);
					$this->view_data['block_desc'] = $this->msec->show_section_desc($id_post);
					$this->view_data['block_id'] = $id_post;
					$this->view_data['level_id'] = $lv_post;
					
					$section = $this->input->post('section');
					
					$result = $this->msec->add_section_to_block($section,$id_post);
					
					if($result->status == 'true'){
						$this->_msg('s',$result->log,'room_section_subject/add_section_to_block/'.$raw_id);
					}elseif($result->status){
						$this->_msg('e',$result->log,'room_section_subject/add_section_to_block/'.$raw_id);
					}
					
				}else{
					$this->view_data['system_message'] = $this->_msg('e',validation_errors());
				}
			}else{
				show_error('Invalid URL Parameters',404);
			}
		}
	}
	
	public function remove_section_from_block($id = FALSE,$auth)
	{
		if($this->authlink->check_hash_make($id,$auth))
		{
			$this->load->model('m_sections');
			if($this->m_sections->remove_section_from_block($id))
			{
				$this->_msg('s','Successfuly Removed section from block.','room_section_subject/block_section_settings#block-sections');
			}else{
				$this->_msg('e','Unable to remove section from Block','room_section_subject/block_section_settings#block-sections');
			}
		
		}else{
			$this->_msg('e','Invalid URL Request Recieved','room_section_subject/block_section_settings#block-sections');
		}
	}
	
	public function delete_block($raw)
	{
		$x = $this->hs->decrypt($raw);
		if($x)
		{
			$id = $x[1];
			$this->load->model('m_sections');
			if($this->m_sections->remove_block($id))
			{
				$this->_msg('s','Successfuly Removed Block.','room_section_subject/block_section_settings#block-sections');
			}else{
				$this->_msg('e','Unable to remove Block','room_section_subject/block_section_settings#block-sections');
			}
		}else{
			$this->_msg('e','Invalid URL Request Recieved','room_section_subject/block_section_settings#block-sections');
		}
	
	}
	
	public function grading_system()
	{
		$this->load->model('M_student_grades','msg');
		$this->view_data['system_message'] = $this->_msg();
		$this->view_data['gd'] = $this->msg->get_grading_system();
		$this->view_data['edit'] = TRUE;
		$this->load->library('grade_converter');
	
		// pp($this->grade_converter->convert(96.6));
		if($this->input->post('create_grading_period'))
		{
			//insert
			$input = $this->input->post();
			unset($input['create_grading_period']);
			// pd($input);
			if($this->msg->create_grading_system($input))
			{
				$this->_msg('s','Successfully Created Grading System','room_section_subject/grading_system');
			}else{
				$this->_msg('e','Unable to Created Grading System','room_section_subject/grading_system');
			}
		
		}elseif($this->input->post('update_grading_period'))
		{
			//update
			$input = $this->input->post();
			unset($input['update_grading_period']);
			// pd($input);
			if($this->msg->update_grading_system($input))
			{
				$this->_msg('s','Successfully Created Grading System','room_section_subject/grading_system');
			}else{
				$this->_msg('e','Unable to Created Grading System','room_section_subject/grading_system');
			}
		
		}
	}
	
}